/**
 * Created by wang on 18/2/7.
 */

$(document).ready(function(){
    $('.flicker-example').flicker();

    var rootPath = $('#rootPath').val();

    //视频加载
    $('.play').on('click',function(){
        var url = $(':input',$(this)).val();
        var text = $('.textyc_sl',$(this).closest('.column')).text();
        layer.open({
            type: 2,
            title: text,
            shadeClose: true,
            shade: 0.8,
            area: ['60%', '90%'],
            content: url //iframe的url
        });
    });

    // 下拉加载  初始化
    $('.scroll-container').infiniteScroll({
        // options
        path: function(){ // 下一页动态地址拼装  相对地址 绝对地址都可以  参数可以自己随便加
            var dataId = $('.scroll-container').children(":last").attr("id");
            var keyword = $('#keyword').val();
            var navType = $('#navType').val();
            var flag = $('#flag').val();

            //顺序很重要
            if(keyword){
                return rootPath+"/nav/web/list?dataId="+dataId+"&title="+keyword;
            }

            if((flag!='1') && navType){
                return rootPath+"/nav/web/list?dataId="+dataId+"&navType="+navType;
            }

            if(keyword && (flag=='1')){
                return rootPath+"/nav/web/list?dataId="+dataId+"&title="+keyword+"&flag=1";
            }else{
                if(keyword){
                    return rootPath+"/nav/web/list?dataId="+dataId+"&title="+keyword;
                }
                if(flag=='1'){
                    return rootPath+"/nav/web/list?dataId="+dataId+"&flag=1";
                }

            }
            return rootPath+"/nav/web/list?dataId="+dataId;
        },
        append: true,
        responseType: "html",
        status : ".scroll-status" // 提示语
    });
    // 下一页加载完成回调
    var instance = InfiniteScroll.data(".scroll-container");
    instance.on("load",function(respond,path){
        var respond = respond.replace(/dataId\":(\d+),/g,"dataId\":\"$1\",");
        respond = $.parseJSON(respond);
        if(respond.errType=='0000'){

            var datas = respond.data.items;
            var html = '';
            for(var a=0;a<datas.length;a++){
                var data = datas[a].item;
                var da = new Date(data.publishTime);
                var year = da.getFullYear()+'年';
                var month = da.getMonth()+1+'月';
                var date = da.getDate()+'日';
                var dateFull = year+month+date;

                var createVideo = function(data){
                    if($.isPlainObject(data)){
                        if(data.video_url){
                        return  '<div class="play">'+
                                '<input type="hidden" value="'+data.video_url+'">'+
                                '<img src="/img/play.png">'+
                                '</div>';
                        }else{
                            return '';
                        }
                    }
                    return '';
                };

                html += '<div class="column" id="'+data.dataId+'">'+
                    '<img src="'+data.img_url[0]+'" />'+createVideo(data)+
                    '<div class="text">'+
                    '<h1>'+
                    '<a target="_blank" href='+rootPath+'"/detail/'+data.dataId+'" class="textyc_sl">'+data.name+'</a>'+
                    '</h1>'+
                    '<span><a href="#">发布时间：'+dateFull+'</a></span>'+
                    '</div>'+
                    '<div class="clearfix"></div>'+
                    '</div>';
            }
        }
        $('.scroll-container').append(html);
        this.isLoading = false; //标识为append结束
    });
});